package com.jumi.microservice.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jumi.microservice.domain.OrderReplenishDTO;
import com.jumi.microservice.entity.LogisticsGoods;
import com.jumi.microservice.entity.OrderGoods;
import org.apache.ibatis.annotations.*;
import org.springframework.scheduling.annotation.Async;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author by mazhao
 * @Classname UserOrderMapper
 * @Description
 * @Date 2020/7/29 15:37
 */
@Mapper
public interface OrderGoodsMapper extends BaseMapper<OrderGoods> {

    /**
     * 批量插入
     *
     * @param orderGoodsList
     * @return
     */
    @Insert("<script>INSERT INTO order_goods " +
            "(`order_id`, `goods_id`, `spu_id`, `is_group`, `goods_name`, `goods_spec`, `goods_amount`, " +
            "`goods_price`, `goods_promotion_price`, `goods_platinum_price`, `goods_diamonds_price`, `goods_king_price`, " +
            "`goods_cost_price`, `goods_gain_price`, " +
            "`goods_label`, `goods_weight`, `goods_freight_fee`, `goods_cover_img`, " +
            "`free_goods`, `drainage`, `is_import`,`fix_rebate`,  `verify_status`, `self_flag`, " +
            "`supplier_id`, `should_warehouse`, `real_warehouse`, `warehouse_sn`, `order_no`, `activity_id`, `activity_type`)" +
            "VALUES " +
            "<foreach collection='list' item='o' index='index' separator=',' >" +
            "(#{o.orderId}, #{o.goodsId}, #{o.spuId}, #{o.isGroup}, #{o.goodsName}, #{o.goodsSpec}, #{o.goodsAmount}, " +
            "#{o.goodsPrice}, #{o.goodsPromotionPrice}, #{o.goodsPlatinumPrice}, #{o.goodsDiamondsPrice}, #{o.goodsKingPrice}, " +
            "#{o.goodsCostPrice}, #{o.goodsGainPrice}, " +
            "#{o.goodsLabel}, #{o.goodsWeight}, #{o.goodsFreightFee}, #{o.goodsCoverImg}, " +
            "#{o.freeGoods}, #{o.drainage}, #{o.isImport}, #{o.fixRebate}, #{o.verifyStatus}, #{o.selfFlag}, " +
            "#{o.supplierId}, #{o.shouldWarehouse}, #{o.realWarehouse}, #{o.warehouseSn}, #{o.orderNo}, #{o.activityId}, #{o.activityType})" +
            "</foreach></script>")
    int batchInsert(List<OrderGoods> orderGoodsList);

    /**
     * 批量更新订单商品实发仓库
     *
     * @param list
     * @return
     */
    @Update("<script><foreach collection='list' item='l' index='index' separator=';'>" +
            " UPDATE order_goods SET" +
            " real_warehouse = #{l.realWarehouse}" +
            " WHERE goods_id = #{l.goodsId} and order_id = #{l.orderId}" +
            " and del_flag = 0 and goods_status = 0" +
            "</foreach></script>")
    int batchUpdateRealWarehouse(List<LogisticsGoods> list);

    /**
     * 批量更新订单商品实发仓库
     *
     * @param list
     * @return
     */
    @Update("<script><foreach collection='list' item='l' index='index' separator=';'>" +
            " UPDATE order_goods SET" +
            " real_warehouse = #{l.warehouseId},should_warehouse = #{l.warehouseId}" +
            " WHERE order_no = #{l.orderSn}" +
            " and del_flag = 0 and goods_status = 0" +
            "</foreach></script>")
    int batchUpdateRealWarehouseByOrderNo(List<OrderReplenishDTO> list);

    /**
     * 批量更新订单商品状态
     *
     * @param list
     * @return
     */
    @Update("<script><foreach collection='list' item='l' index='index' separator=';'>" +
            " UPDATE order_goods SET" +
            " goods_status = if(real_send_amount + #{l.realSendAmount}=goods_amount,2,1),real_send_amount = real_send_amount + #{l.realSendAmount},remark = if(remark!='',CONCAT(remark,'，',#{l.remark}),#{l.remark})" +
            " WHERE goods_id = #{l.goodsId} and order_id = #{l.orderId} and (goods_status = 0 or goods_status = 1)" +
            "</foreach></script>")
    int batchUpdateGoodsStatus(List<OrderGoods> list);

//    /**
//     * 批量更新订单商品状态
//     *
//     * @param list
//     * @return
//     */
//    @Update("<script><foreach collection='list' item='l' index='index' separator=';'>" +
//            " UPDATE order_goods SET" +
//            " goods_status = if(real_send_amount + #{l.realSendAmount}=goods_amount,2,1),real_send_amount = real_send_amount + #{l.realSendAmount},remark = if(remark!='',CONCAT(remark,'，',l.remark),l.remark)" +
//            " WHERE goods_id = #{l.goodsId} and order_id = #{l.orderId}" +
//            "</foreach></script>")
//    int batchUpdateGoodsStatusWdtPush(List<OrderGoods> list);

    /**
     * 批量更新订单商品状态
     *
     * @param list
     * @return
     */
    @Update("<script><foreach collection='list' item='l' index='index' separator=';'>" +
            " UPDATE order_goods SET" +
            " goods_status = #{l.goodsStatus},real_send_amount = #{l.realSendAmount},remark = if(remark!='',CONCAT(remark,'，',#{l.remark}),#{l.remark})" +
            " WHERE goods_id = #{l.goodsId} and order_id = #{l.orderId}" +
            "</foreach></script>")
    int batchUpdateGoodsStatusPartSend(List<OrderGoods> list);

    /**
     * 统计订单销售总额
     *
     * @param goodsName
     * @param goodsStatus
     * @param supplierId
     * @param warehouse
     * @param startTime
     * @param endTime
     * @param orderNo
     * @param goodsIds
     * @return
     */
    @Select("<script>" +
            "select ifnull(sum(goods_price * goods_amount + goods_freight_fee),0) as amount from order_goods " +
            " where (goods_status >=0 or (goods_status = -2 and after_seal_status >0)) " +
            "<if test = 'goodsName !=null and goodsName.trim()!=&quot;&quot;'> and goods_name like CONCAT('%',#{goodsName},'%')" +
            "</if> " +
            "<if test = 'goodsStatus!=null'> and goods_status = #{goodsStatus}" +
            "</if> " +
            "<if test = 'supplierId !=null'> and supplier_id = #{supplierId}" +
            "</if> <if test = 'warehouse!=null'> and real_warehouse = #{warehouse}" +
            "</if>" +
            "<if test = 'startTime!=null'> and create_time &gt;= #{startTime}" +
            "</if><if test = 'endTime!=null'> and create_time &lt;= #{endTime} " +
            "</if>" +
            "<if test = 'orderNo!=null and orderNo.trim()!=&quot;&quot;'> and order_no = #{orderNo}" +
            "</if><if test = 'goodsIds!=null and goodsIds.trim()!=&quot;&quot;'> and goods_id in ( ${goodsIds} )" +
            "</if><if test = 'afterSaleStatus!=null'> and after_seal_status = #{afterSaleStatus}" +
            "</if>" +
            "</script>")
    Map<String, BigDecimal> sumOrderAmount(@Param("goodsName") String goodsName,
                                           @Param("goodsStatus") Byte goodsStatus,
                                           @Param("supplierId") Integer supplierId,
                                           @Param("warehouse") Integer warehouse,
                                           @Param("startTime") Date startTime,
                                           @Param("endTime") Date endTime,
                                           @Param("orderNo") String orderNo,
                                           @Param("goodsIds") String goodsIds,
                                           @Param("afterSaleStatus") Byte afterSaleStatus);

    /**
     * spu销量统计
     *
     * @param spuIds
     * @return
     */
    @Select("select spu_id, sum(goods_amount) as sales_volume from order_goods " +
            "where spu_id in ( ${spuIds} ) and goods_status <> -1 and after_seal_status <> 1 GROUP BY spu_id;")
    List<Map<String, Object>> getSpuSalesVolume(@Param("spuIds") String spuIds);

    /**
     * 用户活动spu销量统计
     *
     * @return
     */
    @Select("<script>" +
            "<foreach collection='orderGoods' item='item' index='index' separator=' union '>" +
            "select #{item.spuId} as spu_id ,count(1) as order_count, ifnull(sum(goods_amount), 0) as sales_volume " +
            "from order_goods " +
            "where spu_id = #{item.spuId} and activity_id = #{item.activityId} and activity_type = #{item.activityType} and create_time > #{item.createTime} and order_id in ( ${orderIds} )" +
            "</foreach></script>")
    List<Map<String, Object>> getSpuSalesVolumeByUidInDate(@Param("orderGoods") List<OrderGoods> orderGoods, @Param("orderIds") String orderIds);

}
